<script lang="ts" setup>
import { nextTick, watch } from 'vue';

import { useVbenForm } from '#/adapter/form';
import { getUsersById } from '#/api/drone/drones';

import { useUsersFormSchema } from '../data';

const props = defineProps<{
  id?: number; // 用户ID（主表的关联字段）
}>();

const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    formItemClass: 'col-span-2',
    labelWidth: 80,
  },
  layout: 'horizontal',
  schema: useUsersFormSchema(),
  showDefaultActions: false,
});

/** 暴露出表单校验方法和表单值获取方法 */
defineExpose({
  validate: async () => {
    const { valid } = await formApi.validate();
    return valid;
  },
  getValues: formApi.getValues,
});

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.id,
  async (val) => {
    if (!val) {
      return;
    }
    await nextTick();
    await formApi.setValues(await getUsersById(props.id!));
  },
  { immediate: true },
);
</script>

<template>
  <Form class="mx-4" />
</template>
